{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "import time\n",
    "import wget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "kw = \"对话框\"\n",
    "count = 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://th.bing.com/th/id/R60f1ac6135bf4b197a63f1bffc2ee331?rik=Fpsrvc5H1O4vLA&riu=http%3a%2f%2fpic40.nipic.com%2f20140418%2f2531170_202612075312_2.jpg&ehk=gJmzJzQ1%2bNk0fUBXl8Vi%2bdRPEmi7dsoTK2IBxz0aRaE%3d&risl=&pid=ImgRaw\n",
      "100% [............................................................................] 109212 / 109212https://th.bing.com/th/id/Rc46cb79a3831bb8744645e5998408f5f?rik=EypeUgGaIDFXdw&riu=http%3a%2f%2fpic147.nipic.com%2ffile%2f20171128%2f26111114_135414526035_2.jpg&ehk=mbC0SDFfr6ldAMkKBuXsQ%2fAYJ5Rq%2fILHIYdqWfemaJM%3d&risl=&pid=ImgRaw\n",
      "100% [............................................................................] 173467 / 173467https://th.bing.com/th/id/R19f217f1993f6d1547b8ccb29cadaf33?rik=UbvXs1Y76xFrqw&riu=http%3a%2f%2fpic256.nipic.com%2ffile%2f20191129%2f29652855_161913765084_2.jpg&ehk=3I08cYjNeO%2fgHdtlXoBDkuaDe8bHIHMlt5ItQNJEJTA%3d&risl=&pid=ImgRaw\n",
      "100% [............................................................................] 223473 / 223473https://th.bing.com/th/id/R1852a67c02abab8d56deff268a9e006d?rik=RJga7l2FFtqsDQ&riu=http%3a%2f%2fpic71.nipic.com%2ffile%2f20150706%2f2531170_201934738000_2.jpg&ehk=Sjbwlx3bBSsY4RfvDqKqZfJnJj%2basqCFXGMObkyHwyQ%3d&risl=&pid=ImgRaw\n",
      "100% [............................................................................] 329811 / 329811https://th.bing.com/th/id/R1205fd9755994f54cfba0eaa50b709dd?rik=gHj5NkVLCvEllA&riu=http%3a%2f%2fpic16.nipic.com%2f20110910%2f2531170_042906730000_2.jpg&ehk=0rsBI3rRNNB%2f3YDn61qr%2bsEfeW6FrhJAxptLiBoT4Sw%3d&risl=&pid=ImgRaw\n",
      "100% [............................................................................] 112693 / 112693https://th.bing.com/th/id/R9f6d1864fe23897a1008d996213d0d64?rik=mqYANFZ78KyUWA&riu=http%3a%2f%2fpic228.nipic.com%2ffile%2f20190617%2f10673188_172214347089_2.jpg&ehk=cxtBT8JDkazW%2foNbvvzYTDVhZ8Zo7KsrQuoJx5FxSDo%3d&risl=&pid=ImgRaw\n",
      "100% [............................................................................] 127106 / 127106https://th.bing.com/th/id/R8861946e4addda3bd7eb0df5e711dd1c?rik=B56KcMGD9uD%2fyg&riu=http%3a%2f%2fpic35.nipic.com%2f20131108%2f2531170_210953253001_2.jpg&ehk=uvnazjtpQV%2bb8Zpi6NcQSD2GzxLijZ1%2bibBbIzQxuJY%3d&risl=&pid=ImgRaw\n",
      "100% [............................................................................] 185163 / 185163https://th.bing.com/th/id/R95908036677e15ce180ea97bf6103cd6?rik=WarrxH6j%2fKKoyQ&riu=http%3a%2f%2fpic43.nipic.com%2f20140711%2f2531170_080153419000_2.jpg&ehk=tJt2mJD%2b%2bRA7veg%2b%2bZT59x8y6s7R9p5oRipYvlc4t3E%3d&risl=&pid=ImgRaw\n",
      "100% [..........................................................................] 1061748 / 1061748https://th.bing.com/th/id/R0df77001e91c8852cec7417714e1e762?rik=%2bscQUQnhlSl8Mg&riu=http%3a%2f%2fpic40.nipic.com%2f20140418%2f2531170_202552012311_2.jpg&ehk=gj2Gfr1%2fJCbVT7X4eR0yWRSgtJVDQG7pJTdGNV5bhTA%3d&risl=&pid=ImgRaw\n",
      "100% [............................................................................] 254279 / 254279https://th.bing.com/th/id/R42dc1c1308e9c69907f718e55f8e576f?rik=T%2bT095ODLpz5bA&riu=http%3a%2f%2fpic41.nipic.com%2f20140509%2f6608733_085602781000_2.jpg&ehk=9wv9LAdh%2fvZ51nAmOazhLUYzamZBEFuMUr6v6Kvh6Ug%3d&risl=&pid=ImgRaw\n",
      "100% [............................................................................] 176977 / 176977"
     ]
    }
   ],
   "source": [
    "b = webdriver.Chrome()\n",
    "page = b.get(\"https://cn.bing.com/images\")\n",
    "time.sleep(0.5)\n",
    "b.find_element_by_id(\"sb_form_q\").send_keys(kw)\n",
    "b.find_element_by_id(\"sb_form_go\").click()\n",
    "\n",
    "imgs = b.find_element_by_id(\"mmComponent_images_1\").find_elements_by_tag_name(\"img\")\n",
    "for i in range(count):\n",
    "    img = imgs[i]\n",
    "    src = img.get_attribute(\"src\")\n",
    "#     print(src)\n",
    "    if (src.startswith(\"https\")):\n",
    "        img.click()\n",
    "        time.sleep(0.3)\n",
    "        iframe = b.find_element_by_id(\"OverlayIFrame\")\n",
    "        b.switch_to.frame(iframe)\n",
    "        finalImg = b.find_element_by_css_selector(\"#mainImageViewer .richImage img\")\n",
    "        finalSrc = finalImg.get_attribute(\"src\")\n",
    "        print(finalSrc)\n",
    "        \n",
    "        filename = str(i) + \".png\"\n",
    "        if (finalSrc.index(\".jpg\") > -1):\n",
    "            filename = str(i) + \".jpg\"\n",
    "        wget.download(finalSrc, \"imgs/\" + kw + \"-\" + filename)\n",
    "        \n",
    "        # back to previous page\n",
    "        b.back()\n",
    "b.quit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
